import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:fluttertoast/fluttertoast.dart';

class RouteHome extends StatelessWidget {
  const RouteHome({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("路由首页"),
      ),
      body: Container(
        child: Column(
          children: [
            Text(
              "navigator方法名中带有name的都是静态路由方法，不带名称的都是动态路由方法！！！",
              style: TextStyle(fontSize: 19.sp),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.push(context, MaterialPageRoute(builder: (cxt) => RoutePageA()));
              },
              child: Text("pushA"),
            ),
            LayoutBuilder(builder: (BuildContext _, constraint1) {
              return Row(
                children: [
                  LayoutBuilder(builder: (BuildContext _, constraint2) {
                    return ConstrainedBox(
                        constraints: constraint1.copyWith(maxWidth: (constraint1.maxWidth-100)),
                        child: Text(
                          "1232"*20,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                        ));
                  }),
                  Text("data")
                ],
              );
            })
          ],
        ),
      ),
    );
  }
}

class RoutePageA extends StatelessWidget {
  const RoutePageA({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("A页面"),
      ),
      body: Container(
        child: Column(
          children: [
            ElevatedButton(
              onPressed: () {
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (cxt) => RoutePageB(), settings: RouteSettings(name: "/B")),
                );
              },
              child: Text("pushB"),
            ),
            Text("动态路由也可以设置名称：settings: RouteSettings(name: \"/B\")"),
            Divider(),
            ElevatedButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text("pop"),
            ),
          ],
        ),
      ),
    );
  }
}

class RoutePageB extends StatelessWidget {
  const RoutePageB({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("B页面"),
      ),
      body: Container(
        child: Column(
          children: [
            ElevatedButton(
              onPressed: () {
                Navigator.push(context, MaterialPageRoute(builder: (cxt) => RoutePageC()));
              },
              child: Text("pushC"),
            ),
            ElevatedButton(
              onPressed: () {
                Navigator.pushReplacement(context, MaterialPageRoute(builder: (cxt) => RoutePageC()));
              },
              child: Text("pushReplacementC"),
            ),
          ],
        ),
      ),
    );
  }
}

class RoutePageC extends StatelessWidget {
  const RoutePageC({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("C页面"),
      ),
      body: Container(
        child: Column(
          children: [
            ElevatedButton(
              onPressed: () {
                Navigator.push(context, MaterialPageRoute(builder: (cxt) => RoutePageD()));
              },
              child: Text("pushD"),
            ),
          ],
        ),
      ),
    );
  }
}

class RoutePageD extends StatelessWidget {
  const RoutePageD({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("D页面"),
      ),
      body: SingleChildScrollView(
        child: Container(
            padding: EdgeInsets.all(10.w),
            child: Column(
              children: [
                ElevatedButton(
                  onPressed: () {
                    Navigator.pushAndRemoveUntil(
                        context, MaterialPageRoute(builder: (cxt) => RoutePageE()), (route) => route.settings.name == '/B');
                  },
                  child: Text("pushAndRemoveUntil(E直接到B前)"),
                ),
                Text(
                    "写法： Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (cxt) => RoutePageD()),route.settings.name=='/B';"),
                Divider(),
                ElevatedButton(
                  onPressed: () {
                    Navigator.pushAndRemoveUntil(
                        context, MaterialPageRoute(builder: (cxt) => RoutePageE()), ModalRoute.withName("/"));
                  },
                  child: Text("pushAndRemoveUntil(E直接到根前)"),
                ),
                Text(
                    "写法： Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (cxt) => RoutePageD()),ModalRoute.withName(\"/\");"),
                Divider(),
                ElevatedButton(
                  onPressed: () {
                    Navigator.pushAndRemoveUntil(
                        context, MaterialPageRoute(builder: (cxt) => RoutePageE()), (route) => route == null);
                  },
                  child: Text("pushAndRemoveUntil(E成为根)"),
                ),
                Text(
                    "写法： Navigator.pushAndRemoveUntil(context, MaterialPageRoute(builder: (cxt) => RoutePageD()),(route)=>route ==null);"),
                Divider(),
                ElevatedButton(
                  onPressed: () {
                    Navigator.popUntil(context, ModalRoute.withName("/"));
                  },
                  child: Text("PopUtil(回到首页)"),
                ),
                Text("写法：Navigator.popUntil(context,ModalRoute.withName(\"/\"));"),
                Divider(),
                ElevatedButton(
                  onPressed: () {
                    Navigator.popUntil(context, (route) => route == null);
                  },
                  child: Text("PopUtil(退出程序；会黑屏)"),
                ),
                Text("写法： Navigator.popUntil(context,(route)=>route==null);"),
                Divider(),
                ElevatedButton(
                  onPressed: () {
                    if (Platform.isIOS) {
                      exit(0);
                    } else {
                      SystemChannels.platform.invokeMethod('SystemNavigator.pop');
                    }
                  },
                  child: Text("退出程序"),
                ),
                Text("写法:SystemChannels.platform.invokeMethod('SystemNavigator.pop'); exit(0);"),
              ],
            )),
      ),
    );
  }
}

class RoutePageE extends StatelessWidget {
  const RoutePageE({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("E页面"),
      ),
      body: Container(
        padding: EdgeInsets.all(10.w),
        child: Column(
          children: [
            ElevatedButton(
              onPressed: () {
                bool result = Navigator.canPop(context);
                Fluttertoast.showToast(msg: "canPop=$result");
              },
              child: Text("canPop"),
            ),
            Text("canPop表示是不是最后一个根路由,如果是根路由返回false;maybePop是他的升级不是根就pop否则不动"),
            Divider(),
            ElevatedButton(
              onPressed: () {},
              child: Text("removeRoute"),
            ),
            Text("当前页面调用类似与pop;区别在于没有过度动画;removeRouteBelow移出指定路由底层临近的路由，"
                "并且路由不存在是不会报错;此二者方法使用时支持有对应的路由，一般用于立即关闭如移除当前页面的弹出框等"),
          ],
        ),
      ),
    );
  }
}
